<?php
/**
 * 后台业务相关基类控制器
 */
declare (strict_types = 1);

namespace app\admin\controller\admin;

use app\BaseController;
use app\admin\model\admin\AdminUser as AdminModel;
use app\common\traits\Jump;
use think\facade\Cache;
use think\facade\Session;
use think\facade\View;
use think\facade\Request;
use app\admin\service\MenuService;

class Base extends BaseController
{
    use Jump;
    
    public function initialize()
    {
        // 验证用户是否登录
        $adminModel = new AdminModel();
        if ($adminModel->isLogin() == false) {
            $this->redirect('login');
        }
        // 获取用户信息
        if( Session::get('admin') != false ){
            View::assign('userInfo',Session::get('admin'));
        }
        // 向模板下发变量
        $method = Request::action();
        View::assign('method',$method);
        if( $method == 'add' ){
            View::assign('formData',[]);
        }
    }

     /**
      * 清除后台缓存
      */
     protected function rm()
     {
        Cache::clear();
        //重新生成菜单缓存
        $uid = Session::get('admin.id');
        Cache::set("admin:{$uid}:menu",MenuService::getListByUid($uid));
     }

}